﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Member.Sys.DAL
{
    public class DAL_Comments
    {
        public DAL_Comments() { }
        /// <summary>
        /// 验证评论用户名
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [CommentUser]");
            strSql.Append(" where Name=@name ");
            SqlParameter[] parameters = {
                    new SqlParameter("@name", SqlDbType.VarChar,50)};
            parameters[0].Value = name;

            return Member.DB.DbHelpers.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 添加评论用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddCommentUser(Member.Sys.Model.Model_Comments model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CommentUser(");
            strSql.Append("Name,Passwd)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Passwd)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar),
					new SqlParameter("@Passwd", SqlDbType.VarChar)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Passwd;
            object obj = Member.DB.DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 添加评论语句
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddComments(Member.Sys.Model.Model_Comments model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Comments(");
            strSql.Append("Messages)");
            strSql.Append(" values (");
            strSql.Append("@Messages)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Messages", SqlDbType.VarChar)};
            parameters[0].Value = model.Comments;
            object obj = Member.DB.DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append("Name,Passwd");
            strSql.Append(" from CommentUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return Member.DB.DbHelpers.GetDataSet(strSql.ToString());
        }

    }
}
